package com.example.common.base.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.common.base.service.BaseService;
import com.example.common.utils.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;

/**
 * @author yukong
 * @date 2019-01-23 10:24
 * S： 业务service实现
 * Q： 查询query实现
 * T:  查询实体类
 * P:  主键类型
 */
public class BaseNoDelController<S extends BaseService<T>,Q extends IPage<T>, T> {

    @Autowired
    protected S baseService;



//    @ApiOperation(value = "添加", httpMethod = "POST")
    @PostMapping
    public ApiResult<Boolean> save(@RequestBody T t){
        return new ApiResult<>(baseService.save(t));
    }

//    @ApiOperation(value = "修改", httpMethod = "PUT")
    @PutMapping
    public ApiResult<Boolean> update(@RequestBody T t){
        return new ApiResult<>(baseService.updateById(t));
    }

//    @ApiOperation(value = "分页查询", notes = "分页查询", httpMethod = "GET")
    @GetMapping("/page")
    public ApiResult<IPage<T>> pageByQuery(Q sysRoleQuery){
        return new ApiResult<>(baseService.pageByQuery(sysRoleQuery));
    }

//    @ApiOperation(value = "查询所有信息", notes = "查询所有信息", httpMethod = "GET")
    @GetMapping
    public ApiResult<Collection<T>> selectAll(){
        return new ApiResult<>(baseService.listByMap(new HashMap<>()));
    }

//    @ApiOperation(value = "通过条件查询列表", notes = "通过条件查询列表", httpMethod = "GET")
    @GetMapping("/listByCondition")
    public ApiResult<List<T>> listByCondition(T t){
//        try {
//            ParameterizedType ptype = (ParameterizedType) this.getClass().getGenericSuperclass();
//            Class clazz = (Class<T>) ptype.getActualTypeArguments()[2];
//            T t = (T) clazz.newInstance();
//            BeanUtils.copyProperties(q,t);
//            QueryWrapper<T> queryWrapper = new QueryWrapper<>();
//            queryWrapper.setEntity(t);
        return new ApiResult<>(baseService.listByCondition(t));
//        }catch (Exception e){
//            e.printStackTrace();
//            return new ApiResult<>(e.getCause());
//        }
    }

}
